#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include <PowerupsX>

#define PREFIX "{olive}PowerupsX {default}>{green} "

new AbilityFreeze;
new g_FreezeSprite;

new Handle:AbilityFreezeTimer[MAXPLAYERS + 1] = { INVALID_HANDLE, ... };

public Plugin:myinfo= 
{
	name="PowerupsXL Freeze",
	author="Skydive",
	description="PowerupsX Freeze Ability",
	version="1.0",
	url=""
};

public OnPluginStart()
{
	NativeIncStart();
	
	AbilityFreeze = PX_RegisterAbility("AbilityFreeze");
	PX_SetAbilityName(AbilityFreeze,"Freeze");
	PX_SetAbilityDescription(AbilityFreeze,"Level 1: Freeze Targets. Duration: Level.");
	PX_SetAbilityUsage(AbilityFreeze,"Freezes Target!");
	PX_SetAbilityMaxLevel(AbilityFreeze,10);

	HookEvent("player_spawn",Event_PlayerSpawn);
	HookEvent("player_hurt",Event_PlayerHurt);
	HookEvent("player_death",Event_PlayerDeath);
}

public OnMapStart()
{
	PrecacheSound("physics/glass/glass_impact_bullet4.wav", true);
	g_FreezeSprite = PrecacheModel("sprites/blueglow2.vmt");
}

public Action:Event_PlayerSpawn(Handle:event, const String:name[],bool:dontBroadcast)
{
	new userid = GetClientOfUserId(GetEventInt(event,"userid"));

	if(AbilityFreezeTimer[userid] != INVALID_HANDLE)
	{
		CloseHandle(AbilityFreezeTimer[userid]);
		FreezePlayer(userid, false);
	}
}

public Action:Event_PlayerHurt(Handle:event, const String:name[],bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event,"userid"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	new Level = PX_GetLevel(attacker,PX_GetAbility(attacker));
	
	if(PX_GetAbility(attacker) == AbilityFreeze && Level > 0)
	{
		if(AbilityFreezeTimer[victim] == INVALID_HANDLE)
		{
			FreezePlayer(victim, true);
			PrintHintText(victim,"You Have Been Frozen! Duration %i seconds", Level);
			AbilityFreezeTimer[victim] = CreateTimer( float(Level), FreezeTimer, victim );
		}
	}
}

public Action:Event_PlayerDeath(Handle:event, const String:name[],bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event,"userid"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	new Level = PX_GetLevel(attacker,PX_GetAbility(attacker));
	
	if(PX_GetAbility(attacker) == AbilityFreeze && Level > 0)
	{
		if(AbilityFreezeTimer[victim] == INVALID_HANDLE)
		{
			FreezePlayer(victim, true);
			PrintHintText(victim,"You Have Been Frozen! Duration %i seconds", Level);
			AbilityFreezeTimer[victim] = CreateTimer( float(Level), FreezeTimer );
		}
	}
}

public Action:FreezeTimer( Handle:hTimer, any:Client )
{
	FreezePlayer(Client, false);
	PrintHintText(Client,"Unfrozen!");
	return Plugin_Continue;
}

stock FreezePlayer(Client, bool:Freeze)
{
	new Float:vec[3];
	GetClientAbsOrigin(Client, vec);
	EmitAmbientSound("physics/glass/glass_impact_bullet4.wav", vec, Client);

	if(Freeze == false)
	{
		SetEntityMoveType(Client, MOVETYPE_WALK);
	}
	else
	{
		TE_SetupGlowSprite(vec, g_FreezeSprite, 0.95, 1.5, 50);
		TE_SendToAll();
		SetEntityMoveType(Client, MOVETYPE_NONE);
	}
}

public OnClientDisconnect(Client)
{
    if(AbilityFreezeTimer[Client] != INVALID_HANDLE)
    {
        KillTimer(AbilityFreezeTimer[Client]);
        AbilityFreezeTimer[Client] = INVALID_HANDLE;
    }
}